﻿// SMIL is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://smil.codeplex.com/



using System;
using System.Linq;
using Microsoft.SharePoint;



namespace SMIL.Server.SPGeoRssCore
{
    public static class ExtendedSPListItem
    {
        public static string GetCoordinate(this SPListItem spListItem, CoordinateLocation cl)
        {
            try
            {
                if (cl.LocalCoordinate)
                    return spListItem[cl.Field.Title] as string;

                // På uppslagskolumner står ID# med i värdet, ex. "123;#Klingsta 1:41" så vi går direkt på ID:t
                int remote_lookupid = int.Parse(((string)spListItem[cl.Field.Id]).Split(new char[] { ';' })[0]);

                return cl.RemoteLookupList.Items[remote_lookupid][cl.RemoteLookupField.Id] as string;
            }
            catch
            {
                return string.Empty;
            }
        }
        public static string GetDisplayName(this SPList spList, Guid listItemGuid)
        {
            return spList.GetItemByUniqueId(listItemGuid).DisplayName;
        }
    }

    public static class ExtendedSPList
    {
        public static CoordinateLocation GetCoordinateLocation(this SPList spList)
        {
            try
            {
                foreach (SPField spField in spList.Fields)
                {
                    if (spField.TypeDisplayName.Equals("SMIL.Coordinate"))
                    {
                        return new CoordinateLocation() { Field = spField };
                    }

                }

                foreach (SPField spField in spList.Fields)
                {
                    if (spField.Type == SPFieldType.Lookup)
                    {
                        SPFieldLookup spl = spField as SPFieldLookup;

                        SPList lookuplist = spList.Lists[new Guid(spl.LookupList)];

                        foreach (SPField spField2 in lookuplist.Fields)
                        {
                            if (spField2.TypeDisplayName.Equals("SMIL.Coordinate"))
                                return new CoordinateLocation() { Field = spField, RemoteLookupList = lookuplist, RemoteLookupField = spField2 };
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return null;
        }

        public static bool ContainsCoordinateType(this SPList spList)
        {
            return spList.GetCoordinateLocation() != null;

        }

        public static string[] GetViews(this SPList spList)
        {
            if (spList == null || (spList == null && spList.Views == null) )
                return new string[0];
        
            string[] views = new string[spList.Views.Count];
            int i = 0;
            foreach (SPView view in spList.Views)
            {
                views[i] = view.Title;
                i++;
            }
            return views;
        }
    }
}
